package com.asj.service.command.specific;

import static com.asj.enums.AppIdentifierDictEnums.no_option;
import static com.asj.enums.DeviceIdentifierDictEnums.json_key_resultMsg;
import static com.asj.enums.DeviceIdentifierDictEnums.restrictAppBlack;
import static com.asj.enums.DeviceIdentifierDictEnums.restrictAppBlackList;
import static com.asj.enums.DeviceIdentifierDictEnums.restrictAppWhiteList;
import static com.asj.enums.DeviceIdentifierDictEnums.restrictBlackResult;
import static com.asj.enums.DeviceIdentifierDictEnums.restrictWhiteResult;
import static com.asj.enums.HttpRequestDictEnums.receiveSettingBlackListInfoResultUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.util.Log;

import com.asj.MainActivity;
import com.asj.service.oppoSdk.OPPOControlService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.HttpRequestUtils;
import com.asj.utils.JSONParamPackingUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 设置黑白名单
 * */
public class SpecificRestrictAppBlackService {

    public static void restrictAppBlack(JsonObject contentData, Context context){
        try{
            setBlacklistAndWhitelist(contentData,context);
            JSONParamPackingUtils.functionPacking(contentData,result_success,null);
        }catch (Exception e){
            Log.e("设置黑白名单异常！", String.valueOf(e));
            JSONParamPackingUtils.functionPacking(contentData,result_error,e.getMessage());
        }
        new Thread(()->{
            //将请求发送回去
            String result = HttpRequestUtils.retryHttpRequest(receiveSettingBlackListInfoResultUrl.getValue(), contentData.toString(), result_success.getCode(), 5,context);
            if(!result.equals(request_error.getMessage())){
                Log.i("设置app黑名单限制已返回结果，告知成功！",result);
            }
        }).start();
    }

    /**
     * 设置根据黑白名单设置应用禁用（分别传输黑白名单的集合，分别针对不同集合放入不同的名单）
     * */
    public static void setBlacklistAndWhitelist(JsonObject contentData, Context context) {
        Type type = new TypeToken<Map<String, List<String>>>() {}.getType();
        Map<String, List<String>> appList = new Gson().fromJson(contentData.get(restrictAppBlack.getValue()).getAsString(), type);

        StringBuilder addAppRestrictionResult = new StringBuilder();
        StringBuilder removeAppRestrictionResult = new StringBuilder();

        OPPOControlService oppoControlService = new OPPOControlService(context);
        appList.forEach((k, v) -> {
            if (k.equals(restrictAppWhiteList.getValue())) {
                //去除所有空格
                //删除黑名单的名单
                oppoControlService.cancelSetBlackList(1, v);
                Log.w("设置的允许使用应用：",v.toString());
                addAppRestrictionResult.append(v);
            }
            if (k.equals(restrictAppBlackList.getValue())) {
                //去除所有空格
                //添加黑名单的名单
                oppoControlService.setBlackList(1, v);
                Log.w("设置的禁用应用：",v.toString());
                removeAppRestrictionResult.append(v);
            }
        });
        contentData.addProperty(restrictBlackResult.getValue(),addAppRestrictionResult.toString());
        contentData.addProperty(restrictWhiteResult.getValue(),removeAppRestrictionResult.toString());
    }

    /**
     * 设置禁用，仅设置黑名单的数据
     * */
    public static void setOnlyBlackList(Context context, List<String> blackList){
        OPPOControlService oppoControlService = new OPPOControlService(context);
        if(!blackList.isEmpty()){
            //添加黑名单的名单
            oppoControlService.setBlackList(1, blackList);
        }
    }
}
